﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KeywordAuction;
using Simulation.Mechanism;
using Simulation.Strategy;

namespace Simulation.Factory
{

    public class AuctionFactoryForChapter6WithGSP : IAuctionHostFactory
    {
        private void createMultipleStrategy(Advertiser advertiser)
        {
            for (int i = 0; i < 6; i++)
            {
                for (int i2 = 0; i2 < 6; i2++)
                {
                    MultipleStrategyContainerStrategy container = new MultipleStrategyContainerStrategy();
                    addMultipleStrategy(i, container.StrategyList);
                    addMultipleStrategy(i2, container.StrategyList);
                    advertiser.Strategies.Add(container);
                }
            }
        }
        public void addMultipleStrategy(int type, List<MultipleStrategyBase> container)
        {
            if (type == 0)
            {
                container.Add(new MultipleStrategyRaise());
            }
            if (type == 1)
            {
                container.Add(new MultipleStrategyDrop());
            }
            if (type == 2)
            {
                container.Add(new MultipleStrategyDropValue() { Value = 5 });
            }
            if (type == 3)
            {
                container.Add(new MultipleStrategyHold());
            }
            if (type == 4)
            {
                container.Add(new MultipleStrategyJump());
            }
            if (type == 5)
            {
                container.Add(new MultipleStrategyRaiseValue() { Value = 5 });
            }
        }

        public AuctionHost Create()
        {
            var auctionHost = new AuctionHost();
            auctionHost.Name = "Chap6.BigSmaillWordAdpostWithGSP";
            auctionHost.AuctionMechanism = new GSPAuctionMechanism();

            for (int i = 0; i < 3; i++)
            {
                var advertiser = auctionHost.CreateAdvertiser();
                auctionHost.Advertisers.Add(advertiser);
                advertiser.BudgetConstraint = 100;
                advertiser.Tactics = new KeywordAuction.Tactics.BreadthFirstTactics();
                createMultipleStrategy(advertiser);
                advertiser.Name = string.Format("Player {0}", i);
            }

            var ctrs = new List<double>();
            ctrs.Add(0.6131);
            ctrs.Add(0.5111);
            var adspot = KeywordAuction.Utility.AdspotFactory.Create(auctionHost, 100, 2, ctrs);
            adspot.Name = "Adspot 0";
            auctionHost.Adspots.Add(adspot);
            var adspot2 = KeywordAuction.Utility.AdspotFactory.Create(auctionHost, 300, 2, ctrs);
            adspot2.Name = "Adspot 1";
            auctionHost.Adspots.Add(adspot2);

            var random = new Random();
        
            foreach (var spot in auctionHost.Adspots)
            {
                double value = 26;
                foreach (var advertiser in auctionHost.Advertisers)
                {
                    var bidding = AdvertiserBidding.CreateAdvertiserBidding(advertiser, spot);
                    bidding.ExpectedRevenue = value;// random.Next(30, 100);
                    if (value == 26)
                    {
                        value = 60;
                    }
                    else
                    {
                        value = 69;
                    }
                    advertiser.Biddings.Add(bidding);
                    spot.Bitting.Add(bidding);
                }
            }
    
            return auctionHost;
        }
    }


    public class AuctionFactoryForChapter6WithGFP : IAuctionHostFactory
    {
        public AuctionHost Create()
        {
            var factory = new AuctionFactoryForChapter6WithGSP();
            var host = factory.Create();
            host.Name = "Chap6.BigSmaillWordAdpostWithGFP";
            host.AuctionMechanism = new GFPAuctionMechanism();
            return host;
        }
    }


    public class AuctionFactoryForChapter6WithLEF : IAuctionHostFactory
    {
        public AuctionHost Create()
        {
            var factory = new AuctionFactoryForChapter6WithGSP();
            var host = factory.Create();

            host.Name = "Chap6.BigSmaillWordAdpostWithLEFGSP";
            host.AuctionMechanism = new LEFGSPAuctionMechanism();
            return host;
        }
    }

    public class AuctionFactoryForChapter6WithVCG : IAuctionHostFactory
    {
        public AuctionHost Create()
        {
            var factory = new AuctionFactoryForChapter6WithGSP();
            var host = factory.Create();

            host.Name = "Chap6.BigSmaillWordAdpostWithVCG";
            host.AuctionMechanism = new VCGAuctionMechanism();
            return host;
        }
    }
}
